package god.dream.user.controller;

import god.dream.comment.model.Comment;
import god.dream.comment.service.CommentService;
import god.dream.notice.model.Notice;
import god.dream.notice.service.NoticeService;
import god.dream.user.model.User;
import god.dream.user.service.UserService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserNotFormController extends MultiActionController {
	@Autowired
	private UserService userService;
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private NoticeService noticeService;
	
	public ModelAndView listUserForLogInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "list_users_for_loginfo";
		ModelAndView mav = extractUserList(url);
		return mav;
	}
	public ModelAndView listUsers(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "list_users";
		List<Notice> noticeList = noticeService.findNoticeList();
		ModelAndView mav = extractUserList(url);
		mav.addObject("notices", noticeList);
		return mav;
	}
	private ModelAndView extractUserList(String url) {
		List<User> userList = userService.findUserList();
		ModelAndView mav = new ModelAndView(url);
		mav.addObject("users", userList);
		return mav;
	}
	public ModelAndView deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		if(id != null)
			userService.removeUser(Integer.parseInt(id));
		ModelAndView mav = new ModelAndView("redirect:/cont/listUsers.htm");
		return mav;
	}
	public ModelAndView selectUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		ModelAndView mav = new ModelAndView("select_user");
		if(id != null){
			User user = userService.findUser(Integer.parseInt(id));
			List<Comment> listComm = commentService.findCommentListWithUser(user.getId());
			mav.addObject("user", user);
			mav.addObject("comments", listComm);
		}
		return mav;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public CommentService getCommentService() {
		return commentService;
	}
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	public NoticeService getNoticeService() {
		return noticeService;
	}
	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}
}
